/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-07-08 16:55:17
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-12 23:15:20
 */
import React, { useEffect, useState } from "react";
import { Space, Radio } from "antd";
import request  from "@/utils/request";
import cmdbres from "@/data/cmdbres";

const NameSpaceBtn = ({setSpace})=>{
	const [info, setInfo] = useState([]);
    const [loading, setInfoLoading] = useState(false);
    const [status, setStatus] = useState();

    useEffect(() => {
        queryNameSpaceList();
    }, []);

    const queryNameSpaceList = async () => {
        try {
            setInfoLoading(true);
            let res = await request({
                ...cmdbres.queryNameSpaceList,
            });
            setInfoLoading(false);
            if (res && res.code == 200) {
                setInfo(res.data);
                res.data.map(i=>{
                    i.label = i.name;
                    i.value = i.namespace;
                });
            }
        } catch (error) {
            setInfo({});
            console.log(error);
        }
    }

    useEffect(() => {
        if(info.length > 0) {
            setStatus(info[0].namespace);
            setSpace(info[0].namespace);
        }
    }, [info]);
    
    const statusChange = e => {
        setStatus(e.target.value);
        setSpace(e.target.value);
    };

    return (
        <>
        {
            info.length>0 ? 
            <Space>
                <label>命名空间：</label>
                <Radio.Group
                    options={info}
                    value={status}
                    defaultValue={status || info[0]?.namespace}
                    onChange={statusChange}
                    optionType="button"
                    buttonStyle="solid"
                />
            </Space> : null
        }
        </>
    );
};

export default NameSpaceBtn;
